package com.aooci.ordercat.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;

import com.aooci.ordercat.dao.RecetteDAO;
import com.aooci.ordercat.domain.Recette;
import com.aooci.ordercat.enums.OrderStatus;
import com.aooci.ordercat.vo.CheckoutVo;
import com.aooci.ordercat.vo.RecetteVo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Account entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.bitongline.lgs.dao.Account
 */
public class RecetteDAOImpl extends AbstractBaseDAOImpl<Recette, Integer> implements RecetteDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7233782559467441458L;

	public RecetteDAOImpl() {
		super(Recette.class);
	}


	@Override
	public List<RecetteVo> selectByAcrossDayConditons(final int selectedYear, final int selectedMonth, final int selectedDay, final int nextYear, final int nextMonth, final int nextDay, final OrderStatus status, final int divideTime) {
	    return getHibernateTemplate().execute(new HibernateCallback<List<RecetteVo>>() {  
	        @SuppressWarnings("unchecked")
			@Override  
	        public List<RecetteVo> doInHibernate(Session session) {  
	        	Query query = session.getNamedQuery("RECETTE.selectByAcrossDayConditons");
	        	query.setParameter("selectedYear", selectedYear);
	        	query.setParameter("selectedMonth", selectedMonth);
	        	query.setParameter("selectedDay", selectedDay);
	        	query.setParameter("nextYear", nextYear);
	        	query.setParameter("nextMonth", nextMonth);
	        	query.setParameter("nextDay", nextDay);
	        	query.setParameter("status", status);
	        	query.setParameter("divideTime", divideTime);
	            return (List<RecetteVo>)query.list();  
	        }  
	    });
	}


	@Override
	public void deleteByOrdercatId(final int ordercatId) {
		getHibernateTemplate().execute(new HibernateCallback<Object>() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				Query query = session.getNamedQuery("RECETTE.deleteByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	        	query.executeUpdate();
				return null;
			}
		});
	}


	@Override
	public Recette findByOrdercatId(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<Recette>() {  
	        @Override  
	        public Recette doInHibernate(Session session) {  
	        	Query query = session.getNamedQuery("RECETTE.findByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	        	@SuppressWarnings("unchecked")
				List<Recette> list = query.list();
	            return list.isEmpty()? null : list.get(0);
	        }  
	    });
	}


	@Override
	public CheckoutVo findCheckouVoByOrdercatId(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<CheckoutVo>() {  
	        @Override  
	        public CheckoutVo doInHibernate(Session session) {  
	        	Query query = session.getNamedQuery("RECETTE.findCheckouVoByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	        	@SuppressWarnings("unchecked")
				List<CheckoutVo> list = query.list();
	            return list.isEmpty()? null : list.get(0);
	        }  
	    });
	}



}